package com.xiaojiang.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.xiaojiang.domain.Cust;
import com.xiaojiang.util.DaoUtils;

public class CustDaoImpl implements CustDao {

	public Cust isName(String name) {
		try{
		String sql = "select *from customer where name=?";
		QueryRunner runner = new QueryRunner(DaoUtils.getSource());
		return runner.query(sql,new BeanHandler<Cust>(Cust.class),name);
		}catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void addCust(Cust cust) {
		try {
//			String sql = "insert into question(qid,qText, option_a ,option_b,option_c,option_d , option_true, cid) values(?,?,?,?,?,?,?,?)";
//			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
//			runner.update(sql,1,cust.getQtext(),cust.getOption_a(),//
			String sql = "insert into question(qid,qText, option_a ,option_b,option_c,option_d , option_true, cid) values(null,?,?,?,?,?,?,?)";
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,cust.getQtext(),cust.getOption_a(),//
					cust.getOption_b(),cust.getOption_c(),//
					cust.getOption_d(),cust.getOption_true(),//
					cust.getCid());//
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List<Cust> getAllCust() {	
		String sql = "select * from question order by qid desc ";
		try {
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanListHandler<Cust>(Cust.class));
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public Cust isCustId(String id) {
		try {
			String sql = "select * from question where qid=?";
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanHandler<Cust>(Cust.class),id);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void updateCust(Cust cust) {
		try {
			
//			`qid` int(11) NOT NULL,
//			  `qText` text COLLATE utf8_bin NOT NULL,
//			  `option_a` varchar(100) COLLATE utf8_bin NOT NULL,
//			  `option_b` varchar(100) COLLATE utf8_bin NOT NULL,
//			  `option_c` varchar(100) COLLATE utf8_bin NOT NULL,
//			  `option_d` varchar(100) COLLATE utf8_bin NOT NULL,
//			  `option_true` varchar(10) COLLATE utf8_bin NOT NULL,
//			  `cid` i
			String sql = "update question set qText=?,option_a=?,option_b=?,"//
					+ "option_c=?,option_d=?,option_true=?,cid=? where qid=?";
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,cust.getQtext(),cust.getOption_a(),//
					cust.getOption_b(),cust.getOption_c(),//
					cust.getOption_d(),cust.getOption_true(),//
					cust.getCid(),cust.getQid());//
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void DelCust(String id) {
		
		try {
			String sql = "delete from question where qid = ?";
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql,id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
	}

}
